/**
 * Created by 任强 on 2017/5/26.
 */
//点击登录弹窗
$(".login").click(function(){
    layer.open({
        title: false,
        type: 1,
        area: ['350px', '260px'], //宽高
        content: '<div class="loginBody">' +
        '<div class="loginBody2">登录</div>' +
        '<div><input class="loginUsername" type="text" maxlength="11" placeholder="请输入手机号" /></div>' +
        '<div><input class="loginPassword" type="password" placeholder="请输入密码" /></div>' +
        '<div class="loginBtn" >登录</div>' +
        '<div class="loginfoot">' +
        '<a href="###" class="loginRegister">注册成为专职主播</a>' +
        '<a href="###" class="loginWpassword">忘记密码</a>' +
        '</div>' +
        '</div>'
    });
    //鼠标经过按钮
    var loginBtn = $(".loginBtn");
    loginBtn.mouseover(function(){
        $(".loginBtn").css("background-color","#009a00");
    });
    loginBtn.mouseout(function(){
        $(".loginBtn").css("background-color","#40a864");
    });
    //点击登录按钮
    loginBtn.click(function(){
        var loginUsername = $(".loginUsername").val();
        var loginPassword = $(".loginPassword").val();
        if(loginUsername == ''){
            layer.tips('手机号不能为空', '.loginUsername');return;
        }
        if(loginPassword == ''){
            layer.tips('密码不能为空', '.loginPassword');return;
        }
        var phonereg = /^1[3|4|5|6|7|8][0-9]\d{4,8}$/;
        if(!phonereg.test(loginUsername) || loginUsername.length != 11) {
            layer.tips('手机号格式不正确', '.loginUsername');return;
        }
        var loding = layer.msg('加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false, area: '200px',time:100000});
        $.ajax({
            url:"/Home/Login/do_login",
            dataType:"json",
            data:{"username":loginUsername,"password":loginPassword},
            type:"POST",
            success:function(data){
                console.log(data);
                if(data.code == '000000'){
                    location.reload();
                }else{
                    layer.close(loding);
                    layer.msg(data.message);
                }
            }
        });
    })
});


//点击注册弹窗
$(".register").click(function(){
    layer.open({
        title: false,
        type: 1,
        area: ['350px', '260px'], //宽高
        content: '<div class="registerBody">' +
        '<div class="registerBody2">注册成为专职主播</div>' +
        '<div><input class="registerUsername" type="text" maxlength="11" placeholder="请输入手机号" />' +
        '<input type="button" class="registerYzm" id="yzm" value="免费获取验证码"  /> ' +
        '</div>' +
        '<div><input class="registerYzm2"  maxlength="4"  type="text" placeholder="请输入验证码" /></div>' +
        '<div class="registerBtn" >注册</div>' +
        '<div class="registerFoot">' +
        '<input type="checkbox" checked="checked" class="registerCheckbox">' +
        '<span  class="registerLook">我已阅读并同意</span>' +
        '<a href="###" class="registerWord"><<听说主播平台服务协议>></a>' +
        '</div>' +
        '</div>'
    });
    //鼠标经过按钮
    var registerBtn = $(".registerBtn");
    registerBtn.mouseover(function(){
        $(".registerBtn").css("background-color","#009a00");
    });
    registerBtn.mouseout(function(){
        $(".registerBtn").css("background-color","#40a864");
    });
    //点击短信倒计时
    var wait=60;
    function time(o) {
        if(wait == 0) {
            o.removeAttribute("disabled");
            o.value="免费获取验证码";
            wait = 60;
        }else{
            o.setAttribute("disabled", true);
            o.value="重新发送(" + wait + ")";
            wait--;
            setTimeout(function() {
                    time(o)
                }, 1000)
        }
    }
    document.getElementById("yzm").onclick=function(){
        var registerUsername = $(".registerUsername").val();
        if(registerUsername == ''){
            layer.tips('手机号不能为空', '#yzm');return;
        }
        var phonereg = /^1[3|4|5|6|7|8][0-9]\d{4,8}$/;
        if(!phonereg.test(registerUsername) || registerUsername.length != 11) {
            layer.tips('手机号格式不正确', '#yzm');return;
        }
        time(this);
    };
    $(".registerBtn").click(function(){
        var registerUsername = $(".registerUsername").val();
        var registerYzm2 = $(".registerYzm2").val();
        if(registerUsername == ''){
            layer.tips('手机号不能为空', '.registerUsername');return;
        }
        if(registerYzm2 == ''){
            layer.tips('验证码不能为空', '.registerYzm2');return;
        }
        var phonereg = /^1[3|4|5|6|7|8][0-9]\d{4,8}$/;
        if(!phonereg.test(registerUsername) || registerUsername.length != 11) {
            layer.tips('手机号格式不正确', '.registerUsername');return;
        }
        var loding = layer.msg('加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false, area: '200px',time:100000});
        $.ajax({
            url:"/Home/Register/do_register",
            dataType:"json",
            data:{"username":registerUsername,"code":registerYzm2},
            type:"POST",
            success:function(data){
                if(data.code == '000000'){
                    window.location.href = data.data.url+'?username='+data.data.username;
                }else{
                    layer.close(loding);
                    layer.msg(data.message);
                }
            }
        });
    })

});